package cn.ll.ModelAndView;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import cn.ll.po.Customer;

public class ExcelView extends AbstractExcelView {

	private Object[] head;

	@Override
	protected void buildExcelDocument(Map model, HSSFWorkbook wb,
			HttpServletRequest arg2, HttpServletResponse response)
			throws Exception {

		// 设置导出格式，导出默认文件名和编码
		String contentType = "application nd.ms-excel;charset=UTF-8";
		response.setContentType(contentType);
		response.setHeader("Content-disposition", "attachment; filename="
				+ java.net.URLEncoder.encode("customer.xls", "UTF-8"));

		List items = (List) model.get("items");

		if (items.isEmpty()) {
			return;
		}

		HSSFSheet sheet = wb.createSheet("Customer");

		this.createHeader(sheet);
		this.createBody(sheet, items);
		
	}

	private void createBody(HSSFSheet sheet, List<Customer> items) {

		for (int i = 0; i < items.size(); i++) {
			HSSFRow row = sheet.createRow(i + 1);
			this.createOneRow(row, items.get(i));
		}

	}

	private void createOneRow(HSSFRow row, Customer customer) {

		row.createCell((short) 0).setCellValue(
				new HSSFRichTextString(customer.getCustomername()));
		row.createCell((short) 1).setCellValue(
				new HSSFRichTextString(customer.getId()));
		row.createCell((short) 2).setCellValue(
				new HSSFRichTextString(customer.getSubdepartment()));
		row.createCell((short) 3).setCellValue(
				new HSSFRichTextString(customer.getUserid()));
		row.createCell((short) 4).setCellValue(
				new HSSFRichTextString(customer.getContinent()));
		row.createCell((short) 5).setCellValue(
				new HSSFRichTextString(customer.getCountry()));
		row.createCell((short) 6).setCellValue(
				new HSSFRichTextString(customer.getCity()));
		row.createCell((short) 7).setCellValue(
				new HSSFRichTextString(customer.getComtype()));
		row.createCell((short) 8).setCellValue(
				new HSSFRichTextString(customer.getTel()));
		row.createCell((short) 9).setCellValue(
				new HSSFRichTextString(customer.getFax()));
		row.createCell((short) 10).setCellValue(
				new HSSFRichTextString(customer.getAddress()));
		row.createCell((short) 11).setCellValue(
				new HSSFRichTextString(customer.getWebsite()));
		row.createCell((short) 12).setCellValue(
				new HSSFRichTextString(customer.getContactorname()));
		row.createCell((short) 13).setCellValue(
				new HSSFRichTextString(customer.getContactgender()));
		row.createCell((short) 14).setCellValue(
				new HSSFRichTextString(customer.getContactorposition()));
		row.createCell((short) 15).setCellValue(
				new HSSFRichTextString(customer.getContactoremail()));
		row.createCell((short) 16).setCellValue(
				new HSSFRichTextString(customer.getContacttel()));
		row.createCell((short) 17).setCellValue(
				new HSSFRichTextString(customer.getContactmobile()));
		row.createCell((short) 18).setCellValue(
				new HSSFRichTextString(customer.getComstyle()));
		row.createCell((short) 19).setCellValue(
				new HSSFRichTextString(customer.getRemarks()));
	}

	private void createHeader(HSSFSheet sheet) {

		HSSFRow header = sheet.createRow(0);
		header.createCell((short) 0).setCellValue(
				new HSSFRichTextString("公司名称"));
		header.createCell((short) 1).setCellValue(
				new HSSFRichTextString("客户编号"));
		header.createCell((short) 2).setCellValue(
				new HSSFRichTextString("二级单位名称"));
		header.createCell((short) 3).setCellValue(
				new HSSFRichTextString("销售人员"));
		header.createCell((short) 4).setCellValue(new HSSFRichTextString("区域"));
		header.createCell((short) 5).setCellValue(new HSSFRichTextString("国家"));
		header.createCell((short) 6).setCellValue(new HSSFRichTextString("城市"));
		header.createCell((short) 7).setCellValue(
				new HSSFRichTextString("公司属性"));
		header.createCell((short) 8).setCellValue(new HSSFRichTextString("电话"));
		header.createCell((short) 9).setCellValue(new HSSFRichTextString("传真"));
		header.createCell((short) 10)
				.setCellValue(new HSSFRichTextString("地址"));
		header.createCell((short) 11)
				.setCellValue(new HSSFRichTextString("网站"));
		header.createCell((short) 12).setCellValue(
				new HSSFRichTextString("主要负责人姓名"));
		header.createCell((short) 13).setCellValue(
				new HSSFRichTextString("主要负责人性别"));
		header.createCell((short) 14).setCellValue(
				new HSSFRichTextString("主要负责人职务"));
		header.createCell((short) 15).setCellValue(
				new HSSFRichTextString("主要负责人电邮"));
		header.createCell((short) 16).setCellValue(
				new HSSFRichTextString("主要负责人电话"));
		header.createCell((short) 17).setCellValue(
				new HSSFRichTextString("主要负责人手机"));
		header.createCell((short) 18).setCellValue(
				new HSSFRichTextString("公司特点"));
		header.createCell((short) 19)
				.setCellValue(new HSSFRichTextString("备注"));

	}

}
